package com.hazy.animalface.view;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.hazy.animalface.R;
import com.hazy.animalface.adapter.UploadStickersAdapter;
import com.hazy.animalface.event.DeleteStickerBagEvent;
import com.hazy.animalface.event.RefreshStickersEvent;
import com.hazy.animalface.module.Sticker;
import com.hazy.animalface.module.StickerBag;
import com.hazy.animalface.presenter.ILoadPresenter;
import com.hazy.animalface.presenter.ILoadPresenterCompl;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by pengjiahui on 2017/4/14 0014.
 * <p/>
 * 下载界面 主要是下载头贴
 */
public class UploadActivity extends AppCompatActivity implements ILoadView {

    @Bind(R.id.toolbar)
    Toolbar toolbar;                         // 工具栏
    @Bind(R.id.server_stickers_list)
    RecyclerView mServerStickersList;        // 头贴包列表

    private ILoadPresenter iLoadPresenter;

    private UploadStickersAdapter uploadStickersAdapter;        // 头贴包适配器
    private List<StickerBag> stickerBagList;                    // 头贴包列表

    private TextView mProgressTextView;
    private AlertDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        // 设置工具栏
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_upload_progress, null);
        mProgressTextView = (TextView) dialogView.findViewById(R.id.uploading_count_tv);
        mProgressTextView.setText(getString(R.string.loading));
        progressDialog = new AlertDialog.Builder(this).setView(dialogView).create();
        progressDialog.show();
        progressDialog.setCanceledOnTouchOutside(false);

        iLoadPresenter = new ILoadPresenterCompl(this);
        iLoadPresenter.onLoad(1);

        stickerBagList = new ArrayList<>();
        mServerStickersList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        uploadStickersAdapter = new UploadStickersAdapter(this, stickerBagList);
        mServerStickersList.setAdapter(uploadStickersAdapter);
    }

    @Override
    public void onLoadResult(List<StickerBag> stickerBags) {
        stickerBagList.clear();
        stickerBagList.addAll(stickerBags);
        uploadStickersAdapter.notifyDataSetChanged();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void deleteStickerBagEvent(DeleteStickerBagEvent event) {
        final StickerBag stickerBag = event.getStickerBag();
        new AlertDialog.Builder(this).setTitle("提示")
                .setMessage("确定要删除「" + stickerBag.getName() + "」头贴包吗?")
                .setNegativeButton("取消", null)
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        iLoadPresenter.removeStickerBag(stickerBag.getName());
                    }
                }).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshStickersEvent(RefreshStickersEvent event) {
        iLoadPresenter.onLoad(1);
    }

    @Override
    public void onLoadSrickerDetail(boolean result, List<Sticker> stickers) {

    }

    @Override
    public void onCreateResult(boolean result, String message) {

    }

    @Override
    public void onUploadResult(boolean result, int progress, String message) {
        EventBus.getDefault().post(new RefreshStickersEvent());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.add_sticker_bag_fab)
    public void onClick() {
        startActivity(new Intent(UploadActivity.this, CreateStickerBagActivity.class));
    }
}
